package com.ruoyi.tzai.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tzai.domain.ApiKeyPool;
import com.ruoyi.tzai.service.IApiKeyPoolService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * API Key池Controller
 * 
 * @author tzai
 * @date 2025-11-12
 */
@Api(tags = "API Key池管理")
@RestController
@RequestMapping("/tzai/api/keypool")
public class ApiKeyPoolController extends BaseController
{
    @Autowired
    private IApiKeyPoolService apiKeyPoolService;

    /**
     * 查询API Key池列表
     */
    @ApiOperation("查询API Key池列表")
    @PreAuthorize("@ss.hasPermi('tzai:api:keypool:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApiKeyPool apiKeyPool)
    {
        startPage();
        List<ApiKeyPool> list = apiKeyPoolService.selectApiKeyPoolList(apiKeyPool);
        return getDataTable(list);
    }

    /**
     * 导出API Key池列表
     */
    @ApiOperation("导出API Key池列表")
    @PreAuthorize("@ss.hasPermi('tzai:api:keypool:export')")
    @Log(title = "API Key池", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ApiKeyPool apiKeyPool)
    {
        List<ApiKeyPool> list = apiKeyPoolService.selectApiKeyPoolList(apiKeyPool);
        ExcelUtil<ApiKeyPool> util = new ExcelUtil<ApiKeyPool>(ApiKeyPool.class);
        util.exportExcel(response, list, "API Key池数据");
    }

    /**
     * 获取API Key池详细信息
     */
    @ApiOperation("获取API Key池详细信息")
    @PreAuthorize("@ss.hasPermi('tzai:api:keypool:query')")
    @GetMapping(value = "/{keyId}")
    public AjaxResult getInfo(@PathVariable("keyId") Long keyId)
    {
        return success(apiKeyPoolService.selectApiKeyPoolByKeyId(keyId));
    }

    /**
     * 新增API Key池
     */
    @ApiOperation("新增API Key池")
    @PreAuthorize("@ss.hasPermi('tzai:api:keypool:add')")
    @Log(title = "API Key池", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApiKeyPool apiKeyPool)
    {
        return toAjax(apiKeyPoolService.insertApiKeyPool(apiKeyPool));
    }

    /**
     * 修改API Key池
     */
    @ApiOperation("修改API Key池")
    @PreAuthorize("@ss.hasPermi('tzai:api:keypool:edit')")
    @Log(title = "API Key池", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApiKeyPool apiKeyPool)
    {
        return toAjax(apiKeyPoolService.updateApiKeyPool(apiKeyPool));
    }

    /**
     * 删除API Key池
     */
    @ApiOperation("删除API Key池")
    @PreAuthorize("@ss.hasPermi('tzai:api:keypool:remove')")
    @Log(title = "API Key池", businessType = BusinessType.DELETE)
	@DeleteMapping("/{keyIds}")
    public AjaxResult remove(@PathVariable Long[] keyIds)
    {
        return toAjax(apiKeyPoolService.deleteApiKeyPoolByKeyIds(keyIds));
    }

    /**
     * 恢复限流状态的Key
     */
    @ApiOperation("恢复限流状态的Key")
    @PreAuthorize("@ss.hasPermi('tzai:api:keypool:edit')")
    @Log(title = "API Key池", businessType = BusinessType.UPDATE)
    @PostMapping("/recover")
    public AjaxResult recoverRateLimitedKeys()
    {
        int count = apiKeyPoolService.recoverRateLimitedKeys();
        return success("成功恢复 " + count + " 个限流Key");
    }
}

